package com.aigao.golf.common.base;

import com.aigao.golf.common.StringEscapeEditor;
import com.aigao.golf.common.mybatis.PageHelper;
import com.aigao.golf.common.result.ResultTo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author QiangHe
 * @date 2018/1/13 13:06
 */
public class PageController<T extends BaseModel<String>> {
    @Autowired
    protected MyBaseService<T> adaptService;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        binder.registerCustomEditor(String.class, new StringEscapeEditor(false, false, false));

    }

    private Class<T> getEntryClass() {
        Type genericSuperclass = getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
            return (Class<T>) types[0];
        }
        return null;
    }

    public ResultTo dataPage(Integer start, Integer length, String orderBy,
                             Map<String, Object> parameterMap) {
        return dataPage(start, length, orderBy,null,parameterMap, getEntryClass(), adaptService);
    }

    public ResultTo dataPage(Integer start, Integer length, String orderBy,
                             Example example,Map<String, Object> parameterMap) {
        return dataPage(start, length, orderBy,example,parameterMap, getEntryClass(), adaptService);
    }

    public ResultTo dataPage(Integer start, Integer length, String orderBy,Example example,
                             Map<String, Object> parameterMap, Class<T> cls, MyBaseService<T> service) {
        example = buildExample(example, parameterMap, orderBy, cls);
        PageHelper.startPage(start, length,orderBy);
        List<T> list = service.selectByExample(example);
        return new ResultTo(new PageInfo<>(list));
    }

    private Example buildExample(Example example, Map<String, Object> parameterMap,
                                 String orderBy, Class<T> cls) {
        if (example == null) {
            example = new Example(cls);
        }
        Example.Criteria criteria = example.createCriteria();
        for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
            String value = entry.getValue().toString();
            int charAt = value.charAt(value.length() - 1);
            if (charAt == '#') {
                criteria.andLike(entry.getKey(), "%" + value.substring(0, value.length() - 1) + "%");
            } else {
                criteria.andEqualTo(entry.getKey(), entry.getValue());
            }
        }
        if (StringUtils.isNotBlank(orderBy)) {
            int charAtOrder = orderBy.charAt(orderBy.length() - 1);
            if (charAtOrder == '#') {
                example.orderBy(orderBy.substring(0, orderBy.length() - 1)).desc();
            } else {
                example.orderBy(orderBy).asc();
            }
        }
        return example;
    }
}
